const mysql = require('mysql');
const con = mysql.createConnection({
    host:"localhost",
    user:'root',
    password:'root',
    database:'hekeda01'
})
con.connect(e=>{
    //e undefind
    if(e){
        console.log("链接失败！错误为："+e);
    }else{
        console.log("链接成功！");
        //查询功能
        // onQuery();
        //添加
        // onAdd();
        //修改数据
        // onEdit();
        //删除
        onDel();
    }
})

const onDel = ()=>{
    let sql = "delete from tb_news where id=?"
    let arr = [2];
    con.query(sql,arr,(e,r)=>{
        if(e){
            console.log("系统错误，"+e);
        }else{
            console.log("删除成功！");
            console.log(r);
        }
    })
}

function onEdit(){
    let sql = "update tb_news set author=?,content=? where id=3"
    let arr = ["沙僧","33333"];
    con.query(sql,arr,(e,r)=>{
        if(e){
            console.log("系统错误，"+e);
        }else{
            console.log("操作成功！");
            console.log(r);
        }
    })
}

function onAdd(){
    let sqlStr = "insert into tb_news(title,author,content) values (?,?,?)";
    let arr = ["新商城开业了！","孙悟空","111111111"];
    con.query(sqlStr,arr,(e,r)=>{
        console.log(e,r);
    })
}

function onQuery(){
    let sqlStr = "select * from tb_news";
    con.query(sqlStr,(e,r)=>{
        console.log(e,r);
    })
}